package com.atguigu.lease.web.app.service.impl;

import com.atguigu.lease.common.context.LoginUserContext;
import com.atguigu.lease.model.entity.*;
import com.atguigu.lease.model.enums.ItemType;
import com.atguigu.lease.model.enums.LeaseStatus;
import com.atguigu.lease.web.app.mapper.*;
import com.atguigu.lease.web.app.service.ApartmentInfoService;
import com.atguigu.lease.web.app.service.BrowsingHistoryService;
import com.atguigu.lease.web.app.service.RoomInfoService;
import com.atguigu.lease.web.app.vo.apartment.ApartmentItemVo;
import com.atguigu.lease.web.app.vo.attr.AttrValueVo;
import com.atguigu.lease.web.app.vo.fee.FeeValueVo;
import com.atguigu.lease.web.app.vo.graph.GraphVo;
import com.atguigu.lease.web.app.vo.room.RoomDetailVo;
import com.atguigu.lease.web.app.vo.room.RoomItemVo;
import com.atguigu.lease.web.app.vo.room.RoomQueryVo;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.extern.log4j.Log4j2;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author liubo
 * @description 针对表【room_info(房间信息表)】的数据库操作Service实现
 * @createDate 2023-07-26 11:12:39
 */
@Service
@Slf4j
@Log4j2
//@SuppressWarnings("all")
public class RoomInfoServiceImpl extends ServiceImpl<RoomInfoMapper, RoomInfo>
        implements RoomInfoService {

    @Autowired
    private RoomInfoMapper roomInfoMapper;
    @Autowired
    private ApartmentInfoService apartmentInfoService;
    @Autowired
    private GraphInfoMapper graphInfoMapper;
    @Autowired
    private FacilityInfoMapper facilityInfoMapper;
    @Autowired
    private AttrValueMapper attrValueMapper;
    @Autowired
    private LabelInfoMapper labelInfoMapper;
    @Autowired
    private PaymentTypeMapper paymentTypeMapper;
    @Autowired
    private LeaseTermMapper leaseTermMapper;
    @Autowired
    private FeeValueMapper feeValueMapper;
    @Autowired
    private LeaseAgreementMapper leaseAgreementMapper;
    @Autowired
    private BrowsingHistoryService browsingHistoryService;

    @Override
    public IPage<RoomItemVo> selectRoomItemVoPageItem(IPage<RoomItemVo> page, RoomQueryVo queryVo) {
        return roomInfoMapper.selectRoomItemVoPageItem(page, queryVo);
    }

    /**
     * 查询房间详情 包括所属公寓信息 以及触发浏览记录
     * @param id
     * @return
     */
    @Override
    public RoomDetailVo selectRoomDetailVoById(Long id) {
        //todo 1. 查询房间详情
        RoomInfo roomInfo = roomInfoMapper.selectRoomDetailVoById(id);
        if (roomInfo == null) {
            return null;
        }
        Long apartmentId = roomInfo.getApartmentId();
        //todo 2. 查询所属公寓信息
        ApartmentItemVo apartmentItemVo = apartmentInfoService.selectApartmentItemVoById(apartmentId);
        //todo 3. 查询图片信息
        List<GraphVo> graphVoList = graphInfoMapper.selectGraphVoListByItemTypeAndItemId(ItemType.ROOM, id);
        //todo 4. 查询配套信息
        List<FacilityInfo> facilityInfoList = facilityInfoMapper.selectFacilityInfoListByRoomId(id);
        //todo 4. 查询属性信息
        List<AttrValueVo> attrValueVoList = attrValueMapper.selectAttrValueVoListByRoomId(id);
        //todo 5. 查询标签列表
        List<LabelInfo> labelInfoList = labelInfoMapper.selectLabelInfoListByRoomId(id);
        //todo 6. 查询可选支付方式
        List<PaymentType> paymentTypeList = paymentTypeMapper.selectPaymentTypeListByRoomId(id);
        //todo 7. 查询租期
        List<LeaseTerm> leaseTermList = leaseTermMapper.selectLeaseTermListByRoomId(id);
        //todo 8. 查询杂费
        List<FeeValueVo> feeValueVoList = feeValueMapper.selectFeeValueListByRoomId(roomInfo.getApartmentId());
        //todo 9. 查询房间是否删除
        LambdaQueryWrapper<LeaseAgreement> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(LeaseAgreement::getRoomId, id)
                .in(LeaseAgreement::getStatus, LeaseStatus.SIGNED,LeaseStatus.WITHDRAWING );
        Long roomStatus = leaseAgreementMapper.selectCount(wrapper);
        //todo 10. 封装数据
        RoomDetailVo appRoomDetailVo = new RoomDetailVo();
        //todo 拷贝数据
        BeanUtils.copyProperties(roomInfo, appRoomDetailVo);
        appRoomDetailVo.setIsDelete(roomInfo.getIsDeleted() == 1);
        appRoomDetailVo.setIsCheckIn(roomStatus > 0);

        appRoomDetailVo.setApartmentItemVo(apartmentItemVo);
        appRoomDetailVo.setGraphVoList(graphVoList);
        appRoomDetailVo.setFacilityInfoList(facilityInfoList);
        appRoomDetailVo.setAttrValueVoList(attrValueVoList);
        appRoomDetailVo.setLabelInfoList(labelInfoList);
        appRoomDetailVo.setPaymentTypeList(paymentTypeList);
        appRoomDetailVo.setLeaseTermList(leaseTermList);
        appRoomDetailVo.setFeeValueVoList(feeValueVoList);

        //todo 11. 保存浏览记录
        browsingHistoryService.saveBrowsingHistory(LoginUserContext.getLoginUser().getUserId(), id);
        //todo 12. 查询的时候 打印日志 保存浏览记录成功
        log.info("保存浏览记录成功");

        return appRoomDetailVo;
    }

    @Override
    public IPage<RoomItemVo> selectRoomItemVoPageItemByApartmentId(IPage<RoomItemVo> page, Long id) {
        return roomInfoMapper.selectRoomItemVoPageItemByApartmentId(page, id);
    }

}




